package com.lcn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lcn.enums.YesOrNo;
import com.lcn.mapper.FriendshipMapper;
import com.lcn.pojo.Friendship;
import com.lcn.pojo.vo.ContactsVO;
import com.lcn.service.FriendshipService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FriendshipServiceImpl implements FriendshipService {

    @Resource
    private FriendshipMapper friendshipMapper;

    /**
     * 查询好友关系
     *
     * @param friendId
     * @param myId
     * @return
     */
    @Override
    public Friendship getFriendship(String friendId, String myId) {
        QueryWrapper<Friendship> queryWrapper = new QueryWrapper<Friendship>()
                .eq("friend_id", friendId)
                .eq("my_id", myId);
        return friendshipMapper.selectOne(queryWrapper);
    }

    /**
     * 查询通讯录
     *
     * @param userId
     * @param isBlack true 是黑名单 false 不是黑名单
     * @return
     */
    @Override
    public List<ContactsVO> queryMyFriends(String userId, boolean isBlack) {
        Map<String,Object> paramsMap = new HashMap<>();
        paramsMap.put("myId", userId);;
        paramsMap.put("isBlack", isBlack);;
        return friendshipMapper.queryMyFriends(paramsMap);
    }

    /**
     * 修改备注
     *
     * @param myId
     * @param friendId
     * @param friendRemark
     */
    @Override
    public void updateRemark(String myId, String friendId, String friendRemark) {
        QueryWrapper<Friendship> queryWrapper = new QueryWrapper<Friendship>()
                .eq("my_id", myId)
                .eq("friend_id", friendId);
        Friendship friendship = new Friendship();
        friendship.setFriendRemark(friendRemark);
        friendship.setUpdatedTime(LocalDateTime.now());
        friendshipMapper.update(friendship, queryWrapper);
    }

    /**
     * 更改黑名单状态
     *
     * @param myId
     * @param friendId
     * @param yesOrNo
     */
    @Override
    public void updateBlack(String myId, String friendId, YesOrNo yesOrNo) {
        QueryWrapper<Friendship> queryWrapper = new QueryWrapper<Friendship>()
                .eq("my_id", myId)
                .eq("friend_id", friendId);
        Friendship friendship = new Friendship();
        friendship.setIsBlack(yesOrNo.type);
        friendship.setUpdatedTime(LocalDateTime.now());
        friendshipMapper.update(friendship, queryWrapper);
    }

    /**
     * 删除好友
     *
     * @param myId
     * @param friendId
     */
    @Override
    @Transactional
    public void delete(String myId, String friendId) {
        // 双向删除
        QueryWrapper<Friendship> deleteSelfWrapper = new QueryWrapper<Friendship>()
                .eq("my_id", myId)
                .eq("friend_id", friendId);

        QueryWrapper<Friendship> deleteOppositeWrapper = new QueryWrapper<Friendship>()
                .eq("my_id", friendId)
                .eq("friend_id", myId);

        friendshipMapper.delete(deleteSelfWrapper);

        friendshipMapper.delete(deleteOppositeWrapper);

    }

}
